import { MockMethod } from 'vite-plugin-mock'
import Mock from 'mockjs'

// 自定义延迟的时间
const delay = (ms: number) => new Promise((resolve) => setTimeout(() => resolve(true), ms))

export default [
  {
    url: '/api/movies',
    method: 'get',
    // rawResponse 支持执行异步操作，并使用 res.end() 返回结果
    rawResponse: async (req, res) => {
      // mock 假数据
      const data = Mock.mock({
        code: 0,
        message: 'success',
        'list|6-15': [
          {
            'id|+1': 1,
            title: '@ctitle(4, 10)',
            starring: '@cname()',
            pub_date: '@date(yyyy-MM-dd)',
            rate: '@natural(0, 4).@natural(1, 9)'
          }
        ]
      })

      // 延迟2秒返回结果
      await delay(2000)

      // 返回结果
      res.end(JSON.stringify(data))
    }
  }
] as MockMethod[]
